home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / VideoToolbox 97.08.16 / VideoToolboxSources / GetScreenDevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-10  |  8.1 KB  |  247 lines  |  [TEXT/CWIE]

  1. /* GetScreenDevice.c
  2.  
  3.     device=GetScreenDevice(n);
  4. returns a handle to the n-th screen, where the MainDevice is the zero-th screen. 
  5. This is similar, but not identical, to the screen number in the Monitors
  6. Control panel.
  7.  
  8.     n=GetScreenIndex(device);
  9. returns the inverse of GetScreenDevice(). 
  10.  
  11.     slot=GetDeviceSlot(device);
  12. gets the "slot" for any screen device. 
  13.  
  14.     device=SlotToScreenDevice(n);
  15. returns a handle to the screen device in slot n. 
  16.  
  17.     device=GetWindowDevice(window);
  18. returns GDHandle of screen with largest intersection with the window's content.
  19.  
  20.     GDHandle    GetDeviceByRefNum(int n);
  21.     GDHandle    GetRectDevice(Rect *r);
  22.  
  23.     void        LocalToGlobalRect(Rect *r);
  24.     void        GlobalToLocalRect(Rect *r);
  25.  
  26. Copyright © 1989-1995 Denis G. Pelli
  27. HISTORY:
  28. 3/20/90        dgp    make compatible with MPW C.
  29. 3/22/90    dgp    changed GetDeviceSlot to use the AuxDCEHandle instead of deducing it
  30.             from the baseAddr of the PixMap. This is a cleaner way to do it.
  31. 4/9/90    dgp    eliminated #define for Mainscrn mispelling in Color.h
  32. 10/17/90 dgp Added AddressToScreenDevice() for compatibility with built-in video on
  33.             the Mac IIci, IIsi, and LC.
  34. 10/18/90 dgp Added LocalToGlobalRect() and GetWindowDevice().
  35. 8/24/91        dgp    Made compatible with THINK C 5.0.
  36. 2/1/92    dgp    Fixed bugs in GetWindowDevice() which resulted in returning garbage GDHandle.
  37. 3/3/92    dgp    In GetScreenDevice(), skip inactive screens.
  38. 8/20/92    dgp    expanded comments of GetDeviceSlot(), noting that it works even with
  39.             built-in video, e.g. on Mac IIci.
  40. 8/26/92    dgp    GetDeviceSlot() now returns -1 if none, since zero is a legal slot.
  41.             GetScreenDevice() first checks for 8-bit QuickDraw().
  42. 9/10/92    dgp    Actually implemented the 8/26 change instead of just changing the 
  43.             documentation. Oops.
  44. 4/17/93    dgp Deleted obsolete AddressToSlot and AddressToScreenDevice.
  45. 5/21/93    dgp    Fixed GetWindowDevice() to support GWorld's.
  46. 8/14/93    dgp    Based on answer from DEVSUPPORT, I cleaned up GetWindowDevice().
  47. 4/11/94    dgp    Added GetRectDevice() based on code extracted from GetWindowDevice().
  48. 11/30/94 dgp added GetDeviceByRefNum().
  49. 6/1/95 dgp moved LocalToGlobalRect() and GlobalToLocalRect() to CenterRectInRect.c
  50. 10/4/95 dgp GetDeviceSlot() nows checks for presence of Slot Manager and returns -1 if it's absent.
  51. 10/14/95 dgp added GetDeviceSlotName(), which works on all Macs, including PCI.
  52. 3/8/97 dgp added caching to GetScreenDevice, to make it faster, especially when called by
  53.             GetScreenIndex.
  54. 4/10/97    dgp    Eliminate stuff that was conditional on !UNIVERSAL_HEADERS.
  55. */
  56. #include "VideoToolbox.h"
  57. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *modelName);
  58. char *GetDeviceSlotName(GDHandle device);
  59.  
  60. Boolean updateScreenDeviceCache=0;
  61.  
  62. GDHandle GetScreenDevice(int n)
  63. // Returns a handle to the n-th screen, where the MainDevice is the zero-th screen.
  64. // Returns NULL if request can't be satisfied.
  65. // The first time we cache all the devices, so subsequent calls will be fast.
  66. // To allow for the remote possibility that someone needs uncached operation,
  67. // there is a global variable "updateScreenDeviceCache" to flush the cache.
  68. // It's not declared in VideoToolbox.h, so add its declaration explicitly to
  69. // your source, if you need to access it.
  70. {
  71.     GDHandle device;
  72.     int i,error;
  73.     long value;
  74.     static Boolean firstTime=1,tooOld;
  75.     static GDHandle screenDeviceTable[MAX_SCREENS];
  76.  
  77.     if(firstTime){
  78.         error=Gestalt(gestaltQuickdrawVersion,&value);
  79.         tooOld=error || value<gestalt8BitQD;
  80.         updateScreenDeviceCache=1;
  81.         firstTime=0;
  82.     }
  83.     if(tooOld || n<0 || n>=MAX_SCREENS)return NULL;
  84.     if(updateScreenDeviceCache){
  85.         screenDeviceTable[0]=GetMainDevice();
  86.         device=GetDeviceList();
  87.         for(i=1;i<MAX_SCREENS && device!=NULL;){
  88.             if(TestDeviceAttribute(device,screenDevice)
  89.                 && !TestDeviceAttribute(device,mainScreen)
  90.                 && TestDeviceAttribute(device,screenActive)){
  91.                     screenDeviceTable[i++]=device;
  92.             }
  93.             device=GetNextDevice(device);
  94.         }
  95.         for(;i<MAX_SCREENS;i++)screenDeviceTable[i]=NULL;
  96.         updateScreenDeviceCache=0;
  97.     }
  98.     return screenDeviceTable[n];
  99. }
  100.  
  101. GDHandle GetDeviceByRefNum(int n)
  102. {
  103.     int j;
  104.     GDHandle device;
  105.  
  106.     for(j=0;;j++){
  107.         device=GetScreenDevice(j);
  108.         if(device==NULL || n==(**device).gdRefNum)break;
  109.     }
  110.     return device;
  111. }
  112.  
  113. int GetScreenIndex(GDHandle device)
  114. // Inverse of GetScreenDevice(). Returns -1 if request can't be satisfied.
  115. {
  116.     int i,error;
  117.     long value;
  118.     static Boolean firstTime=1,tooOld;
  119.  
  120.     if(firstTime){
  121.         error=Gestalt(gestaltQuickdrawVersion,&value);
  122.         tooOld=error || value<gestalt8BitQD;
  123.         firstTime=0;
  124.     }
  125.     if(tooOld)return 0;
  126.     if(device==NULL)return -1;
  127.     for(i=0;i<MAX_SCREENS;i++)if(device==GetScreenDevice(i))return i;
  128.     return -1;
  129. }
  130.  
  131. short int GetDeviceSlot(GDHandle device)
  132. // Gets the "slot" for any screen device, even if it's built-in video, e.g. on Mac
  133. // IIci or Quadra. See 1992 Inside Mac "Processes" page 4-11. Returns -1 if none.
  134. // Zero is a legal slot for built-in video devices.
  135. // WARNING: gives nonsense answer on PCI Macs. Use GetDeviceSlotName() instead.
  136. {
  137.     AuxDCEHandle myAuxDCEHandle;
  138.     SpBlock spBlock;
  139.     Boolean slotMgrPresent;
  140.     
  141.     if(TrapAvailable(_SlotManager))slotMgrPresent=(SVersion(&spBlock)==noErr);
  142.     else slotMgrPresent=0;
  143.     if(!slotMgrPresent || device==NULL || (*device)->gdRefNum==0)return -1;
  144.  
  145.     myAuxDCEHandle=(AuxDCEHandle) GetDCtlEntry((**device).gdRefNum);
  146.     return ((**myAuxDCEHandle).dCtlSlot);
  147. }
  148.  
  149. char *GetDeviceSlotName(GDHandle device)
  150. // Gets the "slot" name for any screen device, even built-in video, on Macs with NuBus, 
  151. // PCI bus, and no bus. Returns "" if none.
  152. // Zero is a legal slot for built-in video devices.
  153. {
  154.     static char slotName[32]="";
  155.     char cardName[32],modelName[32];
  156.     
  157.     if(device==NULL || (*device)->gdRefNum==0)return "";
  158.     GetVideoProperties(device,slotName,cardName,modelName);
  159.     if(strlen(cardName)==0)sprintf(slotName,"%d",(int)GetDeviceSlot(device));
  160.     return slotName;
  161. }
  162.  
  163. GDHandle SlotToScreenDevice(int n)
  164. // Returns a handle to the screen device in slot n.
  165. // Returns NULL if request can't be satisfied.
  166. {
  167.     GDHandle device;
  168.  
  169.     device=GetDeviceList();
  170.     while(device!=NULL) {
  171.         if (TestDeviceAttribute(device,screenDevice) &&
  172.             GetDeviceSlot(device)==n)
  173.                 break;
  174.         device=GetNextDevice(device);
  175.     }
  176.     return device;
  177. }
  178.  
  179. GDHandle GetWindowDevice(WindowPtr window)
  180. // For on-screen window, returns GDHandle of screen with largest intersection with the 
  181. // window's content.
  182. // For off-screen window (i.e. GWorld), it returns the GDHandle of the associated device.
  183. {
  184.     Rect r;
  185.     WindowPtr oldWindow;
  186.     long qD;
  187.  
  188.     if(window==NULL)return NULL;
  189.     Gestalt(gestaltQuickdrawVersion,&qD);
  190.     if(qD>=gestalt32BitQD && (((CWindowPtr)window)->portVersion&0xc001)==0xc001){
  191.         // It's a GWorld iff the portVersion has both high bits set (cGrafPort) and
  192.         // the low bit set (GWorld). See Apple Tech Note “RowBytes Revealed II”.
  193.         return GetGWorldDevice((GWorldPtr)window);
  194.     }
  195.     r=window->portRect;
  196.     GetPort(&oldWindow);
  197.     SetPort(window);
  198.     LocalToGlobalRect(&r);
  199.     SetPort(oldWindow);
  200.     return GetRectDevice(&r);
  201. }
  202.  
  203. GDHandle GetRectDevice(Rect *r)
  204. // Returns GDHandle of screen with largest intersection with the global rect.
  205. {
  206.     Rect overlap;
  207.     GDHandle device,dominantDevice=NULL;
  208.     long area,greatestArea;
  209.     long qD;
  210.  
  211.     if(r==NULL)return NULL;
  212.     Gestalt(gestaltQuickdrawVersion,&qD);
  213.     if(qD<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  214.     device=GetDeviceList();
  215.     greatestArea=0;
  216.     while(device!=NULL){
  217.         if(TestDeviceAttribute(device,screenDevice)
  218.             && TestDeviceAttribute(device,screenActive)){
  219.                 SectRect(r,&(*device)->gdRect,&overlap);
  220.                 area=(long)(overlap.right-overlap.left)*(overlap.bottom-overlap.top);
  221.                 if(area>greatestArea){
  222.                     greatestArea=area;
  223.                     dominantDevice=device;
  224.                 }
  225.             }
  226.         device=GetNextDevice(device);
  227.     }
  228.     return dominantDevice;
  229. }
  230.  
  231. #if 0
  232.     #ifndef __DISPLAYS__
  233.         #include <Displays.h>
  234.     #endif
  235.     /* 
  236.     These enum definitions for use with the cscGetNextResolution driver status call 
  237.     are taken from the A7 draft (Feb 10,'95) of Apple's Designing PCI Cards and Drivers for
  238.     Power Mac Computers. Undoubtedly these enum definitions will appear in some future 
  239.     version of Video.h, perhaps with CodeWarrior 6, at which time they can be deleted from here.
  240.     */
  241.     enum{
  242.         kDisplayModeIDCurrent=0
  243.         ,kDisplayModeIDInvalid=0xffffffff
  244.         ,kDisplayModeIDFindFirstResolution=0xfffffffe
  245.         ,kDisplayModeIDNoMoreResolutions=0xfffffffd
  246.     };
  247. #endif